package com.grandtech.insurance.service.impl;

import com.grandtech.insurance.dao.DbKhdkxxDAO;
import com.grandtech.insurance.dao.DbKhxxDAO;
import com.grandtech.insurance.dao.DbZhjgDAO;
import com.grandtech.insurance.domain.*;
import com.grandtech.insurance.service.DbZhjgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DbZhjgServiceImpl implements DbZhjgService {

    @Autowired
    private DbZhjgDAO dbZhjgDAO;

    @Autowired
    private DbKhxxDAO dbKhxxDAO;

    @Autowired
    private DbKhdkxxDAO dbKhdkxxDAO;


    @Override
    public BhEntity gettime(Map map) {
        return dbZhjgDAO.gettime(map);
    }

    @Override
    public List<DbZhjg> pagelist(Map map) {
        return dbZhjgDAO.pagelist(map);
    }

    @Override
    public int pagelistcount(Map map) {
        return dbZhjgDAO.pagelistcount(map);
    }

    @Override
    public List<DbZhjg> zypagelist(Map map) {
        return dbZhjgDAO.zypagelist(map);
    }

    @Override
    public int zypagelistcount(Map map) {
        return dbZhjgDAO.zypagelistcount(map);
    }

    @Override
    public List<String> timelist(Map map) {
        return dbZhjgDAO.timelist(map);
    }

    @Override
    public BhDtEntity3 getBhDtEntity3(BhDtEntity3 param) {

        //查询用户信息
        DbKhxx canshu1 = new DbKhxx();
        canshu1.setIdcode(param.getIdcode());
        canshu1.setXmbm(param.getXmbm());
        DbKhxx dbKhxx = dbKhxxDAO.getDbKhxxByIdcodeAndXmbm(canshu1);
        if(dbKhxx!=null){
            param.setName(dbKhxx.getName());
            param.setXnzt(dbKhxx.getXnzt());
        }
        //统计地块数量和面积
        List<DbKhdkxx> khdkxxList = dbKhdkxxDAO.getKhdkxxByIdcodeAndXmbm(canshu1);
        int dbzzdksl = 0;
        BigDecimal dbzzmj = new BigDecimal(0);
        if(khdkxxList.size()>0){
            for(DbKhdkxx khdkxx : khdkxxList){
                if("01".equals(khdkxx.getZytype())){
                    dbzzdksl++;
                    dbzzmj = dbzzmj.add(khdkxx.getMj());
                }
            }
        }
        param.setDksl(dbzzdksl);
        param.setMj(dbzzmj);
        param.setMj1(String.format("%.2f", dbzzmj));

        //查询列表
        List<DbZhjg> list = dbZhjgDAO.getlist(param);
        if(list.size()>3){
            List<DbZhjg> list1 = new ArrayList<DbZhjg>();
            int n1 = 0;
            int nn = list.size()-1;
            int n = 0;

            //zztype是否为空，现在灾害影像中没有作物，为防止以后存入作物，按作物分类，特加此判断
            if(param.getZztype()==null || "".equals(param.getZztype())){
                for(int j=0;j<list.size();j++){
                    if(param.getTime().equals(list.get(j).getTime()) && param.getZhlx().equals(list.get(j).getZhlx())){
                        n = j;
                        list.get(j).setGl(1);
                    }
                }
            }else{
                for(int j=0;j<list.size();j++){
                    if(param.getTime().equals(list.get(j).getTime())
                            && param.getZhlx().equals(list.get(j).getZhlx())
                            && param.getZztype().equals(list.get(j).getZztype())){
                        n = j;
                        list.get(j).setGl(1);
                    }
                }
            }

            if(n==n1){
                list1.add(list.get(n1));
                list1.add(list.get(n1+1));
                list1.add(list.get(n1+2));
            }else if(n==nn){
                list1.add(list.get(nn-2));
                list1.add(list.get(nn-1));
                list1.add(list.get(nn));
            }else{
                list1.add(list.get(n-1));
                list1.add(list.get(n));
                list1.add(list.get(n+1));
            }
            param.setList(list1);

        }else{
            if(list.size()>0){
                for(DbZhjg d : list){
                    if(param.getTime().equals(d.getTime()) && param.getZhlx().equals(d.getZhlx())){
                        d.setGl(1);
                    }
                }
            }
            param.setList(list);
        }

        return param;
    }

    @Override
    public BhDtEntity4 getBhDtEntity4(BhDtEntity4 param) {

        //查询用户信息
        DbKhxx canshu1 = new DbKhxx();
        canshu1.setIdcode(param.getIdcode());
        canshu1.setXmbm(param.getXmbm());
        DbKhxx dbKhxx = dbKhxxDAO.getDbKhxxByIdcodeAndXmbm(canshu1);
        if(dbKhxx!=null){
            param.setName(dbKhxx.getName());
            param.setXnzt(dbKhxx.getXnzt());
            param.setQhdm(dbKhxx.getQhdm());
            param.setQhmc(dbKhxx.getQhmc());
        }
        //根据地块编码查询地块
        Map canshu2 = new HashMap();
        canshu2.put("dkbm",param.getDkbm());
        List<Map> dks = dbKhdkxxDAO.getDksjShape(canshu2);
        if(dks.size()>0){
            param.setMj(new BigDecimal(dks.get(0).get("mj").toString()));
            param.setMj1(String.format("%.2f", new BigDecimal(dks.get(0).get("mj").toString())));
        }

        //列表
        BhDtZhDk bhDtZhDk = new BhDtZhDk();
        bhDtZhDk.setTime(param.getTime());
        bhDtZhDk.setZhmc(param.getZhmc());
        List<BhDtZhDk> list = new ArrayList<BhDtZhDk>();
        list.add(bhDtZhDk);
        param.setList(list);

        return param;
    }
}




































